﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace RobotEscape.Entities
{
    public class Exit : EntityBase
    {

        public ImageBrush imageBrush = null;
        public bool bClosed = false;
        public bool gClosed = false;
        public bool rClosed = false;
        public bool yClosed = false;
        public bool allOpen
        {
            get
            {
                return (!bClosed && !gClosed && !rClosed && !yClosed);
            }
        }

        public Exit()
        {
            this.width = 20;
            this.height = 20;
            this.uiElement = new Rectangle();

            imageBrush = new ImageBrush
            {
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Top
            };
        }

        public override void Update(GameTime time)
        {
            Rectangle rect = this.uiElement as Rectangle;
            Canvas.SetLeft(rect, this.PosX);
            Canvas.SetTop(rect, this.PosY);
            rect.Width = this.width;
            rect.Height = this.height;

            WriteableBitmap wbSource = new WriteableBitmap(ResourceHelper.GetBitmap("Assets/images/entities.png"));
            WriteableBitmap wb = new WriteableBitmap(this.width, this.height);
            wb.Invalidate();
            if (bClosed)
                wb.Blit(new Rect(0, 0, this.width, this.height), wbSource, new Rect(0, 80, 20, 20));
            if (gClosed)
                wb.Blit(new Rect(0, 0, this.width, this.height), wbSource, new Rect(20, 80, 20, 20));
            if (rClosed)
                wb.Blit(new Rect(0, 0, this.width, this.height), wbSource, new Rect(40, 80, 20, 20));
            if (yClosed)
                wb.Blit(new Rect(0, 0, this.width, this.height), wbSource, new Rect(60, 80, 20, 20));
            wb.Invalidate();


            var imageBrush = new ImageBrush
            {
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Top
            };
            imageBrush.ImageSource = wb;
            rect.Fill = imageBrush;
        }
    }
}
